package com.gko100.dao.impl;

import java.util.List;


import com.gko100.dao.UserDao;
import com.gko100.dao.basehibernatesupport.BaseHibernateDaoSupport;
import com.gko100.model.User;

public class UserDaoHibernate extends BaseHibernateDaoSupport implements UserDao {

	public void delete(User user) {
		
		this.getHibernateTemplate().delete(user);
		
	}

	public List<User> findUserAll() {
		
		List<User> userList = this.getHibernateTemplate().find("from User as u order by u.permState");
		
		if(userList!=null&&userList.size()>=1){
			System.out.println("userList.size========="+userList.size());
			return userList;
		}else{
			
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---userList======null");
			return null;
		}
	}

	public User findUserByName(String name) {
		
		List<User> userList = this.getHibernateTemplate().find("from User as u where u.name=?", name);
		
		if(userList!=null&&userList.size()==1){

			return userList.get(0);
			
		}else{
			
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---user======null");
			return null;
		}

	}

	public User findUserByNameAndPass(String name, String password) {
		
		List<User> userList = this.getHibernateTemplate().find("from User as u where u.name=? and u.password=?", name, password);
			
		if(userList!=null&&userList.size()==1){

			return userList.get(0);
			
		}else{
			
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---user======null");
			return null;
		}

	}

	public User findUserByUserId(Integer userId) {
		
		User user = this.getHibernateTemplate().get(User.class, userId);
		if(user!=null){
			return user;
		}else{
			
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---user======null");
			return null;
		}
		
	}

	public void save(User user) {
		
		this.getHibernateTemplate().save(user);
		
	}

	public void update(User user) {
		
		this.getHibernateTemplate().update(user);
		
	}

}
